getwd(9)
getwd()
getwd()
setwd("/Users/mustafayahsi/Downloads/The Seeds of Success - The Pivotal Role of First Round Cooperation in Public Goods Games/")
allcitiesp <- read.csv("Stata/Stata Produced Data/herrmannallcitiesp.csv")
getwd()
k<-read_excel("Stata/Stata Produced Data/oursaverage.xlsx",col_names = FALSE)
library(readxl)
k<-read_excel("Stata/Stata Produced Data/oursaverage.xlsx",col_names = FALSE)
k
k<-as.data.frame(k)
tavg<-matrix(NA,15,10)
for(i in 1:10){
for(j in 1:15){
tavg[j,i]=k[j+15*(i-1),3]
}
}
write.xlsx(tavg,file="R/R produced data/trueavg_demo.xlsx")
############### R-CODE1: RUN ONLY AFTER RUNNING PART1 of STATA CODE ###############
###Appendix Figure 1
### constructing herrmann 1st-10th period data
#install plyr package
#install.packages("plyr")
library(plyr)
#install.packages("ggplot2")
library(ggplot2)
# set working directory
setwd("/Users/mustafayahsi/Desktop/The Seeds of Success - The Pivotal Role of First Round Cooperation in Public Goods Games/")
allcitiesp <- read.csv("Stata/Stata Produced Data/herrmannallcitiesp.csv")
a<-allcitiesp
### Rearranging data so that 1st & 10th period avg cont are on the same row
a$city<-as.factor(a$city)
numrow<-nrow(a)/2
b<-array(NA,dim=c(numrow,5))
j<-1
k<-count(a$city=="Athens")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=1
}
j<-k+1
k<-k+count(a$city=="Bonn")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=2
}
j<-k+1
k<-k+count(a$city=="Boston")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=3
}
j<-k+1
k<-k+count(a$city=="Chengdu")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=4
}
j<-k+1
k<-k+count(a$city=="Copenhagen")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=5
}
j<-k+1
k<-k+count(a$city=="Dnipropetrovs'k")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=6
}
j<-k+1
k<-k+count(a$city=="Istanbul")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=7
}
j<-k+1
k<-k+count(a$city=="Melbourne")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=8
}
j<-k+1
k<-k+count(a$city=="Minsk")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=9
}
j<-k+1
k<-k+count(a$city=="Muscat")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=10
}
j<-k+1
k<-k+count(a$city=="Nottingham")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=11
}
j<-k+1
k<-k+count(a$city=="Riyadh")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=12
}
j<-k+1
k<-k+count(a$city=="Samara")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=13
}
j<-k+1
k<-k+count(a$city=="Seoul")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=14
}
j<-k+1
k<-k+count(a$city=="St. Gallen")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=15
}
j<-k+1
k<-k+count(a$city=="Zurich")[2,2]/2
for(i in j:k){
b[i,1]=a$secseq[2*(i-1)+1]
b[i,2]=a$group_avg[2*(i-1)+1]
b[i,3]=a$group_avg[2*i]
b[i,5]=a$grp[2*(i-1)+1]
b[i,4]=16
}
## Rename columns
colnames(b)<-c("secseq","group_avg1","group_avg2","City","group")
b<-as.data.frame(b)
b$City<-as.factor(b$City)
b$secseq<-as.factor(b$secseq)
b$city2<-b$City
b$City<-revalue(b$City, c("1"="Athens", "2"="Bonn","3"="Boston", "4"="Chengdu","5"="Copenhagen", "6"="Dnipropetrovs'k","7"="Istanbul",
"8"="Melbourne","9"="Minsk", "10"="Muscat","11"="Nottingham", "12"="Riyadh","13"="Samara",
"14"="Seoul","15"="St. Gallen", "16"="Zurich"))
#### Figure 6: First-Period and Last-Period Contributions by City (Herrmann et al., 2008) ######
b2<-subset(b,b$City=="Minsk" | b$City=="Muscat" | b$City=="Riyadh" | b$City=="Samara" | b$City=="Athens" | b$City=="Dnipropetrovs'k")
b2<-as.data.frame(b2)
b2$City<-as.factor(b2$City)
p1<-ggplot(b2,aes(x=group_avg1,y=group_avg2))+facet_wrap(~City)+labs(y = "Contribution - 10th Period",x="Contribution - 1st Period")+theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)+scale_y_continuous(breaks = c(0,5,10,15,20),label = c("0","5","10","15","20"))+
scale_x_continuous(limits=c(0, 20),breaks = c(0,5,10,15,20),label = c("0","5","10","15","20"))+scale_shape_manual(values=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16))
p2<-p1+geom_point()+theme(strip.background = element_blank(), strip.text.x = element_blank())+scale_color_discrete(guide=FALSE)
p3<-p2+geom_abline(slope=1,intercept=0,linetype="dashed")+geom_smooth(method='lm',formula=y~x,se=TRUE,color="black")
p4<-p3+theme(strip.background = element_blank(), strip.text.x = element_blank())
p5<-p4+theme_bw()+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
p5+ theme(legend.position="bottom")
write.csv(b,"R/R Produced Data/allcities_trial.csv")
write.csv(b,"/R/R Produced Data/allcities_trial.csv")
write.csv(b,"allcities_trial.csv")
path_out = 'R/R Produced Data/'
write.csv(b,paste(path_out,'allcities_trial.csv'))
getwd()
path_out = '/Users/mustafayahsi/Downloads/The Seeds of Success - The Pivotal Role of First Round Cooperation in Public Goods Games/R/R Produced Data/'
write.csv(b,paste(path_out,'allcities_trial.csv'))
write.csv(b,"R-Studio/R Produced Data/allcities_trial.csv")
write.csv(b2,"R-Studio/R Produced Data/6cities_trial.csv")
sixcitiesp <- read.csv("Stata/Stata Produced Data/herrmann-fe6cities.csv")
library(plyr)
library(ggplot2)
sixcitiesp <- read.csv("Stata/Stata Produced Data/herrmann-fe6cities.csv")
d<-sixcitiesp
##### Figure A.1: First-Period and Last-Period Contributions by City (Herrmann et al., 2008) ######
p1<-ggplot(d,aes(x=group_avg1,y=group_avg2,shape=city))+labs(y = "Contribution - 10th Period",x="Contribution - 1st Period")+theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)+scale_y_continuous(breaks = c(0,5,10,15,20),label = c("0","5","10","15","20"))+
scale_x_continuous(limits=c(0, 20),breaks = c(0,5,10,15,20),label = c("0","5","10","15","20"))+scale_shape_manual(values=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16))
p2<-p1+geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci),fill="grey")
p3<-p2+geom_point()+theme(strip.background = element_blank(), strip.text.x = element_blank())+scale_color_discrete(guide=FALSE)
p4<-p3+geom_abline(slope=1,intercept=0,linetype="dashed")
p5<-p4+geom_line(aes(y=fitted))
p6<-p5+theme(strip.background = element_blank(), strip.text.x = element_blank())
p7<-p6+theme_bw()+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
p7+ theme(legend.position="bottom")
### Figure 5: Comparison of Average Contributions in the P-experiment ######
#### herrmann part of the graph
b3<-subset(b,b$City=="Istanbul" )
b3<-as.data.frame(b3)
b3$City<-as.factor(b3$City)
p1<-ggplot(b3,aes(x=group_avg1,y=group_avg2))+labs(y = "Contribution - 10th Period",x="Contribution - 1st Period")+theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)+scale_y_continuous(breaks = c(0,5,10,15,20),label = c("0","5","10","15","20"))+
scale_x_continuous(limits=c(0, 20),breaks = c(0,5,10,15,20),label = c("0","5","10","15","20"))+scale_shape_manual(values=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16))
p2<-p1+geom_point()+theme(strip.background = element_blank(), strip.text.x = element_blank())+scale_color_discrete(guide=FALSE)
p3<-p2+geom_abline(slope=1,intercept=0,linetype="dashed")+geom_smooth(method='lm',formula=y~x,se=TRUE,color="black")
p4<-p3+theme(strip.background = element_blank(), strip.text.x = element_blank())
p5<-p4+theme_bw()+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
p5+ theme(legend.position="bottom")
########### Figure 5: Our Data Part of the Graph
#### Constructing trueavg for our data
#install.packages(readxl)
library(readxl)
k<-read_excel("Stata/Stata Produced Data/oursaverage.xlsx",col_names = FALSE)
k<-as.data.frame(k)
tavg<-matrix(NA,15,10)
for(i in 1:10){
for(j in 1:15){
tavg[j,i]=k[j+15*(i-1),3]
}
}
write.xlsx(tavg,file="R-Studio/R produced data/trueavg_trial.xlsx")
library(writexl)
write.xlsx(tavg,file="R-Studio/R produced data/trueavg_trial.xlsx")
library(openxlsx)
write.xlsx(tavg,file="R-Studio/R produced data/trueavg_trial.xlsx")
